package com.netease.connectionpool.mypool;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 封装Connection，提供基本的SQL查询能力
 * 数据库连接管道，就是对JDBC Connection进行封装而已，但是需要注意busy的这个标示。
 * 连接池中的关闭连接实际上是将连接放回到连接池中以便其他使用者复用，实际上只是标示的改变而已！
 */
public class MyPooledConnection {

    private Connection connection;

    private boolean busy;

    public MyPooledConnection(Connection connection, boolean busy) {
        this.connection = connection;
        this.busy = busy;
    }

    public void close() {
        this.busy = false;
    }

    public Connection getConnection() {
        return connection;
    }

    public void setConnection(Connection connection) {
        this.connection = connection;
    }

    public boolean isBusy() {
        return busy;
    }

    public void setBusy(boolean busy) {
        this.busy = busy;
    }

    public ResultSet query(String sql) {
        Statement statement;
        ResultSet resultSet = null;
        try {
            statement = connection.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultSet;
    }
}
